package com.qa.dom.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Author: XiaoBingBy
 * Email: XiaoBingBy@qq.com
 * Date: 2017/9/1
 * Time: 00:00
 * Describe: 获取日期时间工具类
 */
public class DateUtil {

    /**
     * 获取年份
     * @return
     */
    public static int getYear() {

        Calendar cal = Calendar.getInstance();

        return  cal.get(Calendar.YEAR);
    }

    /**
     * 获取日期
     * @return
     */
    public static int getMonth() {
        Calendar cal = Calendar.getInstance();

        return  cal.get(Calendar.MONTH)+1;
    }
    private final static SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
    private final static SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");
    private final static SimpleDateFormat sdfInputDay = new SimpleDateFormat("yyyy.MM.dd");
    private final static SimpleDateFormat sdfDays = new SimpleDateFormat("yyyyMMdd");
    private final static SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    private final static SimpleDateFormat sdfHour = new SimpleDateFormat("HH:mm");
    private final static SimpleDateFormat sdfTimeStr = new SimpleDateFormat("yyyyMMddHHmmss");
    private final static SimpleDateFormat sdfMonth = new SimpleDateFormat("yyyy-MM");
    private final static SimpleDateFormat sdfSecond = new SimpleDateFormat("HHmmss");
    private final static SimpleDateFormat sdfYtoS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private final static SimpleDateFormat day = new SimpleDateFormat("dd");
    /**
     * 根据所传日期格式化  获取YYYY-MM-DD HH:mm格式
     * @param date
     * @return
     */
    public static String getsdfMonthStr(Date date){
        return sdfMonth.format(date);
    }
    public static String getsdfDayStr(Date date){
        return sdfDay.format(date);
    }
    public static String getTime(Date date){
        return sdfTime.format(date);
    }
    public static String getYtoS(Date date){
        return sdfYtoS.format(date);
    }
    public static String getDay(Date date){
        return day.format(date);
    }
    public static Date returnDate(String timestr) throws ParseException{
        return sdfTime.parse(timestr);
    }
    public static Date returnDateYtoS(String timestr) throws ParseException{
        return sdfYtoS.parse(timestr);
    }
    /**
     * 根据所传日期格式化  获取HH:mm格式
     * @param date
     * @return
     */
    public static String getHour(Date date){
        return sdfHour.format(date);
    }

    public static String getDatetime(Date date){
        return sdfTimeStr.format(date);
    }

    /**
     * 获取当前YYYYMMDD格式
     *
     * @return
     */
    public static String returnStrDays(){
        return sdfDays.format(new Date());
    }
    /**
     * 获取当前YYYY-MM-DD格式
     *
     * @return
     */
    public static String getDay() {
        return sdfDay.format(new Date());
    }
    /**
     * 获取当前yyyyMMddHHmmss格式
     *
     * @return
     */
    public static String getTimeStr() {
        return sdfTimeStr.format(new Date());
    }
    /**
     * 获取当前YYYY-MM-DD格式的date类型
     *
     * @return
     * @throws ParseException
     */
    public static Date getDayFormat(String str) throws ParseException{
        return sdfDay.parse(str);
    }

    /**
     * 获取当前YYYY.MM.DD格式的date类型
     *
     * @return
     * @throws ParseException
     */
    public static Date getInputDayFormat(String str) throws ParseException {
        if(str.equals("")){
            return null;
        }
        return sdfInputDay.parse(str);
    }
    /**
     * 获取当前HHmmss格式
     *
     * @return
     */
    public static String getSecond(){
        return sdfSecond.format(new Date());
    }

    public static Date jsontimeToDate(String time){
        Date d = new Date();
        d.setTime(Long.parseLong(time));
        return d;
    }

    public static boolean compareDate(Date s, Date e) {
        return s.getTime() >=e.getTime();
    }

    /**
     * 天数差
     * @param beginDateStr，beginDateStr
     * @return
     * @throws ParseException
     */
    public static long getDaySub(String beginDateStr,String endDateStr){
        long day=0;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate = null;
        Date endDate = null;
        try {
            beginDate = format.parse(beginDateStr);
            endDate= format.parse(endDateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        day=(endDate.getTime()-beginDate.getTime())/(24*60*60*1000);
        return day;
    }



    /**
     * 获得指定日期n天之后的日期(yyyy-MM-dd格式)
     * @param days
     * @return
     * @throws ParseException
     */
    public static String getAfterDayDate(String dateStr,int days) throws ParseException {
        Calendar canlendar = Calendar.getInstance(); // java.util包
        canlendar.setTime(sdfDay.parse(dateStr));
        canlendar.add(Calendar.DATE, days); // 日期减 如果不够减会将月变动
        Date date = canlendar.getTime();
        return sdfDay.format(date);
    }
    public static String getNowYYYYMMDD(){
        return sdfDay.format(new Date());
    }

    /**
     * 比较日期
     * 大于
     * @return
     */
    public static boolean compareDates(Date s, Date e) {
        return s.getTime() >e.getTime();
    }

    /**
     * 倒计时（据当前时间还剩多少）
     * @return 毫秒数
     */
    public static Long returnLasttime(Date d1,Date d2){
        long lasttime = 0;
        if(compareDates(d2,d1)){
            return lasttime;
        }
        return d1.getTime()-d2.getTime();
    }

    public static void main(String[] args) throws ParseException {
        System.out.println( getAfterDayDate("2018-01-31",7*4));
    }
}
